Designing Elixir Systems With OTP by James Edward Gray II
Author:James Edward Gray II
Language: eng
Format: epub
Tags: Pragmatic Bookshelf
Publisher: Pragmatic Bookshelf
Try the Quiz Manager
In this section, we’re going to put the quiz through its paces. Part of writing good code is building the infrastructure to support learning and exploration. We’re going to create a trivial module to let new users and developers alike explore our features in the console.
Let’s create a simple math quiz with mastery of two and a template for single-digit addition. None of this code will be new to you:
Boundary/lib/mastery/examples/math.ex
defmodule Mastery.Examples.Math do
alias Mastery.Core.Quiz
def template_fields() do
[
name: :single_digit_addition,
category: :addition,
instructions: "Add the numbers",
raw: "<%= @left %> + <%= @right %>",
generators: addition_generators(),
checker: &addition_checker/2
]
end
def addition_checker(substitutions, answer) do
left = Keyword.fetch!(substitutions, :left)
right = Keyword.fetch!(substitutions, :right)
to_string(left + right) == String.trim(answer)
end
def addition_generators() do
%{left: Enum.to_list(0..9), right: Enum.to_list(0..9)}
end
def quiz_fields() do
%{ mastery: 2, title: :simple_addition}
end
def quiz() do
quiz_fields()
|> Quiz.new
|> Quiz.add_template(template_fields())
end
end
We have separate functions for quiz and template fields. We also have a function to roll that up into a new quiz. Now we have a few tools that will make our module easy to test. Open up a new IEx session, or at least recompile. Then, you can alias the modules we’ll need, like this:
iex(1)> alias Mastery.Examples.Math
Mastery.Examples.Math
iex(2)> alias Mastery.Boundary.QuizManager
Mastery.Boundary.QuizManager
We alias Math, the example quiz we just built for convenience, and QuizManager, the server layer for building quizzes. Now, we need to start the quiz:
iex(3)> GenServer.start_link QuizManager, %{}, name: QuizManager
{:ok, #PID<0.123.0>}
We need to be able to access our server, perhaps from a web layer so we’ll need to be able to reference it by name. We’ll use the name of the module, which means we’ll only have one copy of QuizManager. The start_link has three arguments, the module that has the GenServer implementation, the empty map that will eventually contain our quizzes, and options. We use the :name option to specify the name for our new server. Now, we can use it:
iex(4)> QuizManager.build_quiz title: :quiz
:ok
iex(5)> QuizManager.add_template :quiz, Math.template_fields
:ok
You can see the smoother API we offer from this layer. We build a quiz, strictly with functions:
iex(6)> QuizManager.lookup_quiz_by_title :quiz
%Mastery.Core.Quiz{ ... }
Nice! That much works. We can see the individual fields of the quiz we added. That’s more than half of our server. Now admin users can establish new quizzes. It’s time to switch to the rest of our server layer, the part for taking quizzes and answering questions.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12585)
Hello! Python by Anthony Briggs(9924)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9799)
The Mikado Method by Ola Ellnestam Daniel Brolund(9783)
Dependency Injection in .NET by Mark Seemann(9347)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8308)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7770)
Grails in Action by Glen Smith Peter Ledbrook(7703)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7564)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7134)
Microservices with Go by Alexander Shuiskov(6896)
Practical Design Patterns for Java Developers by Miroslav Wengner(6813)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6755)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6422)
Angular Projects - Third Edition by Aristeidis Bampakos(6171)
The Art of Crafting User Stories by The Art of Crafting User Stories(5695)
NetSuite for Consultants - Second Edition by Peter Ries(5624)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5437)
Kotlin in Action by Dmitry Jemerov(5071)
